package com.cleaningcloud.erp.controller;

import com.cleaningcloud.common.core.domain.R;
import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import com.cleaningcloud.erp.api.domain.ErpWareCommodityNumber;
import com.cleaningcloud.erp.api.domain.ErpWareHouse;
import com.cleaningcloud.erp.service.IErpWareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 仓库管理Controller
 *
 * @author cleaningcloud
 * @date 2024-09-26
 */
@RestController
@RequestMapping("/ErpWareHouse")
public class ErpWareHouseController extends BaseController {
    @Autowired
    private IErpWareHouseService erpWareHouseService;

    /**
     * 查询仓库管理列表
     */
    @GetMapping("/list")
    public AjaxResult list(ErpWareHouse erpWareHouse) {
        List<ErpWareHouse> list = erpWareHouseService.selectErpWareHouseList(erpWareHouse);
        return success(list);
    }


    /**
     * 根据仓库id查询库存商品数量
     */
    @GetMapping("/getCommodityNumberList")
    public AjaxResult getCommodityNumberList(ErpWareCommodityNumber erpWareHouse) {
        List<ErpWareCommodityNumber> list = erpWareHouseService.getCommodityNumberList(erpWareHouse);
        return success(list);
    }

    /**
     * 导出仓库管理列表
     */
    @RequiresPermissions("erp:ErpWareHouse:export")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpWareHouse erpWareHouse) {
        List<ErpWareHouse> list = erpWareHouseService.selectErpWareHouseList(erpWareHouse);
        ExcelUtil<ErpWareHouse> util = new ExcelUtil<ErpWareHouse>(ErpWareHouse.class);
        util.exportExcel(response, list, "仓库管理数据");
    }

    /**
     * 获取仓库管理详细信息
     */
    @RequiresPermissions("erp:ErpWareHouse:query")
    @GetMapping(value = "/{wareId}")
    public AjaxResult getInfo(@PathVariable("wareId") Long wareId) {
        return success(erpWareHouseService.selectErpWareHouseByWareId(wareId));
    }


    /**
     * 根据项目id获取
     */
    @GetMapping(value = "/getWareHouseByProjectId/{projectId}")
    public ErpWareHouse getWareHouseByProjectId(@PathVariable Long projectId) {
        return erpWareHouseService.selectErpWareHouseByProjectId(projectId);
    }

    /**
     * 根据项目id获取
     */
    @GetMapping(value = "/getWareHouseByPostId/{post}")
    public ErpWareHouse getWareHouseByPostId(@PathVariable Long post) {
        return erpWareHouseService.selectErpWareHouseByPostId(post);
    }

    /**
     * 新增仓库管理
     */
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpWareHouse erpWareHouse) {
        return toAjax(erpWareHouseService.insertErpWareHouse(erpWareHouse));
    }


    /**
     * 批量新增仓库管理
     */
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping("/batchAddWare")
    public R<Boolean> batchAddWare(@RequestBody List<ErpWareHouse> list) {
        return R.ok(erpWareHouseService.batchAddWare(list));
    }

    /**
     * 修改仓库管理
     */
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @RequestMapping("/updateWareNameByPostId")
    public AjaxResult updateWareNameByPostId(@RequestBody ErpWareHouse erpWareHouse) {
        return toAjax(erpWareHouseService.updateWareNameByPostId(erpWareHouse));
    }


    /**
     * 修改仓库管理
     */
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpWareHouse erpWareHouse) {
        return toAjax(erpWareHouseService.updateErpWareHouse(erpWareHouse));
    }


    /**
     * 删除仓库管理
     */
    @RequiresPermissions("erp:ErpWareHouse:remove")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wareIds}")
    public AjaxResult remove(@PathVariable Long[] wareIds) {
        return toAjax(erpWareHouseService.deleteErpWareHouseByWareIds(wareIds));
    }


    /**
     * 删除仓库管理
     */
    @GetMapping("/closureWareHouse/{projectId}")
    public AjaxResult closureWareHouse(@PathVariable Long projectId) {
        return toAjax(erpWareHouseService.closureWareHouse(projectId));
    }


}
